package com.zjx.ssmdemo.ssm.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.zjx.ssmdemo.ssm.entity.User;
import com.zjx.ssmdemo.ssm.service.IUserService;
import com.zjx.ssmdemo.ssm.utils.LogUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

import java.util.Date;
import java.util.List;

@Api(value = "user")
@Controller
@RequestMapping(path = "/usercontroller")
public class UserController {
     @Autowired
     private IUserService userService;

     @ResponseBody
     @RequestMapping(path ="/getqueryAll",method = RequestMethod.GET)
     @ApiOperation(value = "GET请求得到所有用户信息",httpMethod ="GET",notes = "get all user",response = User.class)
     public List<User> getqueryAll(ModelMap modelMap){
         return userService.queryAll();
     }
     @ResponseBody
     @RequestMapping(path ="/postqueryAll",method = RequestMethod.POST)
     public List<User> postqueryAll(){
          return userService.queryAll();
     }

     @ResponseBody
     @RequestMapping(path ="/getqueryId",method = RequestMethod.GET)
     public String getqueybyId(HttpServletRequest request){
                    JSONObject json=new JSONObject();
              if(request.getParameter("id")==null){
                   json.put("state",500);
                   json.put("message","请输入传入id");
                   return json.toJSONString();
              }
              try{
                   User user=userService.querybyId(Integer.parseInt(request.getParameter("id")));
                   if(user==null){
                        json.put("state",500);
                        json.put("message","内容为空");
                        return json.toJSONString();
                   }else{
                        json.put("state",200);
                        json.put("message","有内容");
                        json.put("data",JSONObject.parseObject(JSON.toJSONString(user)));
                        return json.toJSONString();
                   }
              }catch (Exception e){
                  e.printStackTrace();
                   json.put("state",400);
                   json.put("message","异常");
                   return json.toJSONString();
              }

     }
     @ResponseBody
     @RequestMapping(path ="/getinsertUser",method = RequestMethod.GET)
     public String getinsertUser(ModelMap modelMap){
          JSONObject json=new JSONObject();
           User user=new User();
             user.setRegTime(new Date());
             user.setPassword("zhujuxning1996");
             user.setEmail("1010101010@qq");
             user.setRegIp("102.13.1.444");
             user.setRole("23121");
             user.setStatus(9);
                 try {
                     userService.insertUser(user);
                      json.put("state",200);
                      json.put("message","插入完成");
//                      json.put("data",JSONObject.parseObject(JSON.toJSONString(user)));
                      return json.toJSONString();
                 }catch (Exception e){
                      e.printStackTrace();
                      json.put("state",400);
                      json.put("message","异常");
                      return json.toJSONString();
                 }


     }

    @ResponseBody
    @RequestMapping(path ="/getupdateUserbyId",method = RequestMethod.GET)
    public String getupdateUserbyId(ModelMap modelMap, @RequestParam long userId){
        JSONObject json=new JSONObject();
        try {
            if(userId==0){
                json.put("state",500);
                json.put("message","ID不能为空");
                return json.toJSONString();
            }
           int updaterow=userService.updateUserbyId(userId,"我日去你");
            LogUtils.info("更新行数："+updaterow);
            switch (updaterow){
                case 0:
                    json.put("state",400);
                    json.put("message","查无该ID，没有更新");
                    break;

                 default:
                     json.put("state",200);
                     json.put("message","更新完成");
                     break;
            }
//                      json.put("data",JSONObject.parseObject(JSON.toJSONString(user)));
            return json.toJSONString();
        }catch (Exception e){
            e.printStackTrace();
            json.put("state",400);
            json.put("message","异常");
            return json.toJSONString();
        }


    }
}
